import scrapy
import json

class HuyaSpider(scrapy.Spider):
    name = 'huya'
    # allowed_domains = ['huay.com']
    start_urls = ['https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page=2']

    def start_requests(self):
        for i in range(1,4):
            url = 'https://www.huya.com/cache.php?m=LiveList&do=getLiveListByPage&gameId=1663&tagAll=0&page={}'.format(i)
            yield scrapy.Request(url=url)

    def parse(self, response):
        print(response.text)
        # self.logger.error()

        items = json.loads(response.text)
        data  = items.get('data').get('datas')
        xxx = list()
        for i in data:
            datas = {}
            datas['roomName'] = i.get('roomName')
            datas['nick'] = i.get('nick')
            datas['avatar180'] = i.get('avatar180')
            xxx.append(datas)

        # 数据保存json格式  w 覆盖 a追加
        with open('ss.json','a',encoding='utf-8') as f:
            f.write(json.dumps(xxx,ensure_ascii=False))
            f.write('\r\n')

